package com.bjtct.plugin.autogen.wizards.priovider;

import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableItem;

import com.bjtct.plugin.autogen.db.ColumnWrap;
import com.bjtct.plugin.autogen.wizards.consts.Consts;

public class CellModifyer implements ICellModifier {
	private TableViewer tv;

	public CellModifyer(TableViewer tv) {
		super();
		this.tv = tv;
	}

	@Override
	public boolean canModify(Object arg0, String arg1) {

		return true;
	}

	@Override
	public Object getValue(Object o, String p) {
		ColumnWrap col = (ColumnWrap) o;

		if (p.equals("column_name")) {
			return col.getColumn_name();
		} else if (p.equals("column_alis")) {
			return col.getColumn_alias();
		} else if (p.equals("column_comment")) {
			return col.getColumn_comment();
		} else if (p.equals("column_default")) {
			return col.getColumn_default();
		} else if (p.equals("data_type")) {
			return Consts.getIndex(p, Consts.JAVA_TYPE);
		} else if (p.equals("is_nullable")) {			 
			return Consts.getIndex(p, Consts.PAGE_TYPE);

		} else if (p.equals("list_query")) {
			return Consts.getIndex(p, Consts.YES_NO);
		} else if (p.equals("list_show")) {
			return Consts.getIndex(p, Consts.YES_NO);
		} else if (p.equals("update_show")) {
			return Consts.getIndex(p, Consts.YES_NO);
		}else if (p.equals("new_show")) {
			return Consts.getIndex(p, Consts.YES_NO);
		}

		return null;
	}

	@Override
	public void modify(Object e, String p, Object v) {
		// TODO Auto-generated method stub
		TableItem it = (TableItem) e;
		ColumnWrap col = (ColumnWrap) it.getData();
		if (p.equals("column_name")) {
			col.setColumn_name((String) v);
		} else if (p.equals("column_alis")) {
			col.setColumn_alias((String) v);
		} else if (p.equals("column_comment")) {
			col.setColumn_comment((String) v);
		} else if (p.equals("column_default")) {
			col.setColumn_default((String) v);
		} else if (p.equals("data_type")) {
			Integer index = (Integer) v;
			if (index.intValue() == -1) {
				index = 0;
			}
			col.setData_type(Consts.JAVA_TYPE[index]);
			
		} else if (p.equals("is_nullable")) {
			Integer index = (Integer) v;
			if (index.intValue() == -1) {
				index = 0;
			}
			col.setIs_nullable(Consts.PAGE_TYPE[index]);
			
		} else if (p.equals("list_query")) {

			Integer index = (Integer) v;
			if (index.intValue() == -1) {
				index = 0;
			}
			col.setList_query(Consts.YES_NO[index]);
		} else if (p.equals("list_show")) {
			Integer index = (Integer) v;
			if (index.intValue() == -1) {
				index = 0;
			}
			col.setList_show(Consts.YES_NO[index]);

		} else if (p.equals("update_show")) {
			Integer index = (Integer) v;
			if (index.intValue() == -1) {
				index = 0;
			}
			col.setUpdate_show(Consts.YES_NO[index]);
		}else if(p.equals("new_show")){
			Integer index = (Integer) v;
			if (index.intValue() == -1) {
				index = 0;
			}
			col.setNew_show(Consts.YES_NO[index]);
		}

		tv.update(it, null);
		tv.refresh();
	}

}
